<?php
function db_connect($autocommit = true) {
   $result = new mysqli(DB_HOST, DB_USER, DB_PASSWORD);
   if ($result->connect_error) {
     die("connect mysql error");
   } else {
   	 $result->set_charset("utf8");
   	 $result->autocommit($autocommit);
     return $result;
   }
}

/*
将xml转换为数组
*/
function xmlParser($data){
	$parser = xml_parser_create();
	xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
	xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
	xml_parse_into_struct($parser, $data, $columns);
	xml_parser_free($parser);
	return $columns;
}

function validateParam($value,$type,$mixed = 0){
	switch($type){
		case "integer":
			return is_numeric ($value);
		case "strlength":
			if( is_array ($mixed) && count($mixed) > 1){//0,10
				return  strlen($value) >= $mixed[0] && strlen($value) <= $mixed[1];
			}else{//10
				return  strlen($value) > 0 && strlen($value) <= $mixed;
			}
		case "intlength":
			if( is_array ($mixed) && count($mixed) > 1){//0,10
				return  $value >= $mixed[0] && $value <= $mixed[1];
			}else{//10
				return  $value > 0 && $value <= $mixed;
			}
		case "date":
			$regex = "/^(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)$/s"; //日期部分
			if(preg_match($regex,$value)){
				return true;
			}else{
				return false;
			}
		case "phone":
			$regex = "/^([1][3-8]\d{9})$/u";
			if(preg_match($regex,$value)){
				return true;
			}else{
				return false;
			}
		default:
			return false;
	}
}

function getMessage($type){
	switch($type){
		case "integer":
			return ERROR_INTEGER;
		case "strlength":
			return  ERROR_TEXT_LENGTH;
		case "intlength":
			return  ERROR_INT_LENGTH;
		case "date":
			return ERROR_DATE;
		case "phone":
			return ERROR_PHONE;
		case "empty":
			return ERROR_EMPTY;
		default:
			return "";
	}
}

function check_email_regex($email){
	if (preg_match('/^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/u', $email)) {
		return true;
	} else {
		return false;
	}
}


function check_date_regex($value){
	$regex = "/^(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)$/s"; //日期部分
	if(preg_match($regex,$value)){
		return true;
	}else{
		return false;
	}
}

function check_phone_regex($phone){
	if(preg_match("/^[1][3-8]+\d{9}$/u", $phone)) {
		return true;
	} else {
		return false;
	}
}

function set_ret_json($ret_code, &$json_arr){
	switch($ret_code){
	case SUCCESS_NO:
		$json_arr['ret_code'] = SUCCESS_NO;
		$json_arr['ret_message'] = SUCCESS;
		break;
	case ERROR_NO_DATA_INVALD:
		$json_arr['ret_code'] = ERROR_NO_DATA_INVALD;
		$json_arr['ret_message'] = ERROR_DATA_INVALD;
		break;
	case ERROR_NO_EMAIL_BE_REGISTER:
		$json_arr['ret_code'] = ERROR_NO_EMAIL_BE_REGISTER;
		$json_arr['ret_message'] = ERROR_EMAIL_BE_REGISTER;
		break;
	case ERROR_NO_DB_EXEC:
		$json_arr['ret_code'] = ERROR_NO_DB_EXEC;
		$json_arr['ret_message'] = ERROR_DB_EXEC;
		break;
	case LOGIN_FAILED_NO:
		$json_arr['ret_code'] = LOGIN_FAILED_NO;
		$json_arr['ret_message'] = LOGIN_FAILED;
		break;
	case ERR_NO_SEND_EMAIL_BINDING:
		$json_arr['ret_code'] = ERR_NO_SEND_EMAIL_BINDING;
		$json_arr['ret_message'] = ERR_SEND_EMAIL_BINDING;
		break;
	case ERROR_NO_VERIFY_CODE:
		$json_arr['ret_code'] = ERROR_NO_VERIFY_CODE;
		$json_arr['ret_message'] = ERROR_VERIFY_CODE;
		break;
	case ERR_NO_EMAIL_NOT_BINDING:
		$json_arr['ret_code'] = ERR_NO_EMAIL_NOT_BINDING;
		$json_arr['ret_message'] = ERR_EMAIL_NOT_BINDING;
		break;
	case ERR_NO_SEND_CODE_TIMES:
		$json_arr['ret_code'] = ERR_NO_SEND_CODE_TIMES;
		$json_arr['ret_message'] = ERR_SEND_CODE_TIMES;
		break;
	case ERR_NO_NOT_VALID:
		$json_arr['ret_code'] = ERR_NO_NOT_VALID;
		$json_arr['ret_message'] = ERR_NOT_VALID;
		break;
	case ERR_NO_SEND_MESS:
		$json_arr['ret_code'] = ERR_NO_SEND_MESS;
		$json_arr['ret_message'] = ERR_SEND_MESS;
		break;
	default:
		$json_arr['ret_code'] = ERROR_NO_NOT_DEFINE;
		$json_arr['ret_message'] = ERROR_NOT_DEFINE;
	}
}

function generate_keycode( $length = 8 ) {
    // 密码字符集，可任意添加你需要的字符
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $keycode = '';
    for ( $i = 0; $i < $length; $i++ ) 
    {
        $keycode .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }
    return $keycode;
}

function generate_verify_code( $length = 6 ) {
    // 密码字符集，可任意添加你需要的字符
    $chars = '0123456789';
    $keycode = '';
    for ( $i = 0; $i < $length; $i++ ) 
    {
        $keycode .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }
    return $keycode;
}

function getClientIp() {  
	$unknown = 'unknown';  
	if ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown) ) {  
		$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];  
	} elseif ( isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown) ) {  
		$ip = $_SERVER['REMOTE_ADDR'];  
	}
	if (false !== strpos($ip, ','))  
		$ip = reset(explode(',', $ip));  
	return $ip;  
} 
//根据ip定位城市
function getUserLocation() {
	$ip = getClientIp();
	$url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={$ip}";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:8.8.8.8', 'CLIENT-IP:8.8.8.8'));  //构造IP
	curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com/ ");   //构造来路
	//curl_setopt($ch, CURLOPT_HEADER, 1);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	$out = curl_exec($ch);
	curl_close($ch);
	$jsonArr = json_decode($out, true);
	if(1 == $jsonArr["ret"]){
		return array("country" => $jsonArr["country"],"province" => $jsonArr["province"],"city" => $jsonArr["city"]);
	}
	return false;
}

function check_str_include($included,$including){   
	$tmparray = explode($included,$including);   
	if(count($tmparray)>1){   
		return true;   
	}else{   
		return false;   
	}   
}

?>